#include "faketemperaturemodule.h"
#include "canmessage.h"
#include <QDebug>
FakeTemperatureModule::FakeTemperatureModule(quint8 sourceAddress, SerialPortManager &port, QObject *parent)
    : QObject(parent)
    , SourceAddress(sourceAddress)
    , Temperature(INVLAID_TEMPERATURE)
    , MessageSendTimer()
    , PortManager(port)
{
    connect(&MessageSendTimer, SIGNAL(timeout()), this, SLOT(SendMessage()));
    MessageSendTimer.setInterval(1000);
}

void FakeTemperatureModule::StartSending()
{
    MessageSendTimer.start();
    SendMessage();
}

void FakeTemperatureModule::StopSending()
{
    MessageSendTimer.stop();
}

void FakeTemperatureModule::SendMessage()
{
    QVector<quint8> data;
    data.push_back(0xFF);
    data.push_back(Temperature + 100);
    data.push_back(0xFF);
    data.push_back(0xFF);
    data.push_back(0xFF);
    data.push_back(0xFF);
    data.push_back(0xFF);
    data.push_back(0xFF);
    CanMessage mess(0x18000000 | (TEMPERATURE_STATUS_PGN << 8) | SourceAddress, data);
    PortManager.SendLine(mess.GetAsSnifferSendString());

}

void FakeTemperatureModule::SetTemperature(int temp)
{
    qDebug() << "Setting temperature to " << temp;
    Temperature = temp;
}

void FakeTemperatureModule::SetState(bool state)
{
    if(state)
    {
        StartSending();
    }
    else
    {
        StopSending();
    }
}
